package com.java2207.group1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.mapper.EntertainmentMapper;
import com.java2207.group1.pojo.Entertainment;
import com.java2207.group1.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("entertainmentService")
public class EntertainmentServiceImpl implements EntertainmentService {

    @Autowired
    private EntertainmentMapper entertainmentMapper;
    
    @Override
    public LayuiPageVo<Entertainment> entertainmentPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        // mybatis 的分页查询
        PageHelper.startPage(page,limit);

        List<Entertainment> entertainments = entertainmentMapper.selectByParams(params);

        // mybatis 的分页数据结构
        PageInfo<Entertainment> entertainmentPageInfo = new PageInfo<>(entertainments);

        // 把 mybatis 的分页数据封装到 layui 的分页数据结构中
        LayuiPageVo<Entertainment> entertainmentLayuiPageVo = new LayuiPageVo<>();

        entertainmentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        entertainmentLayuiPageVo.setMsg("分页列表数据");  // 提示信息
        entertainmentLayuiPageVo.setCount(entertainmentPageInfo.getTotal());  // 总记录数
        entertainmentLayuiPageVo.setData(entertainmentPageInfo.getList());  // 分页的列表数据
        return entertainmentLayuiPageVo;
    }

    @Override
    public int addEntertainment(Entertainment entertainment) {

        return entertainmentMapper.addEntertainment(entertainment);
    }

    @Override
    public int deleteEntertainment(Integer id) {
        return entertainmentMapper.deleteEntertainment(id);
    }

    @Override
    public int updateEntertainment(Entertainment entertainment) {
        return entertainmentMapper.updateEntertainment(entertainment);
    }

    @Override
    public int batchEntertainment(String ids) {
        String[] arr = ids.split(",");
        return entertainmentMapper.batchEntertainment(arr);
    }
}
